package com.ruoyi.app.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.app.aspect.PermissionAspect;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.dto.Area;
import com.ruoyi.app.dto.CollectDto;
import com.ruoyi.app.dto.DelMessageDto;
import com.ruoyi.app.dto.UserWxLoginReqDto;
import com.ruoyi.app.service.OrderService;
import com.ruoyi.app.service.UserService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.utils.PermissionAnnotate;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/*
@description:用户信息
@ClassName UserController
@author chen
@create 2025-03-13 22:42
@Version 1.0
*/
@Slf4j
@RestController
@RequestMapping("app")
@AllArgsConstructor
public class UserController extends BaseController
{
    final UserService userService;
    final RedisCache redisCache;
    final OrderService orderService;


    /*
     * @==============History===============<br/>
     * @Description //   用户手机一键登录
     * @Date 2025-4-8 19:52
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PostMapping(value = "/phoneLogin",produces = "application/json;charset=utf-8")
    public AjaxResult phoneLogin(@RequestBody User user) throws Exception
    {
        return userService.phoneLogin(user);
    }



    /*
     * @==============History===============<br/>;
     * @Description //   修改用户信息
     * @Date 2025/3/20
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户更新个人信息", sourceType = "controller")
    @PostMapping(value = "/updateUserInfo",produces = "application/json;charset=utf-8")
    public AjaxResult updateUserInfo(@RequestBody User user)
    {
        return userService.updateUserInfo(user);
    }


    /*
     * @param user
     * @==============History===============<br/>;
     * @Description //   小程序注册
     * @Date 2025/3/13
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @PostMapping(value = "/insertUserInfo",produces = "application/json;charset=utf-8")
    public AjaxResult insertUserInfo(@RequestBody User user)
    {
        return userService.insertUserInfo(user);
    }

    /*
     * @param user
     * @==============History===============<br/>;
     * @Description //   小程序登录
     * @Date 2025/3/13
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @PostMapping(value = "/login",produces = "application/json;charset=utf-8")
    public AjaxResult login(@RequestBody User user)
    {
        return userService.login(user);
    }



    /*
     * @==============History===============<br/>;
     * @Description //  根据token获取小程序登录信息
     * @Date 2025/3/14
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "获取用户信息", sourceType = "controller")
    @GetMapping(value = "/getUserInfo",produces = "application/json;charset=utf-8")
    public AjaxResult getUserInfo()
    {
        return AjaxResult.success(PermissionAspect.loginUser.get());
    }

    /*
     * @==============History===============<br/>
     * @Description //   微信授权
     * @Date 2025-3-16 0:23
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "微信授权", sourceType = "controller")
    @PostMapping(value = "/wx/login",produces = "application/json;charset=utf-8")
    public AjaxResult wxLogin(@RequestBody UserWxLoginReqDto wxLoginReqDto)
    {
        return userService.wxLogin(wxLoginReqDto);
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户退出登录
     * @Date 2025-3-18 21:27
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户退出登录", sourceType = "controller")
    @GetMapping(value = "/logout",produces = "application/json;charset=utf-8")
    public AjaxResult logout()
    {
        String invitationCode = PermissionAspect.loginUser.get().getInvitationCode();
        return redisCache.deleteObject(CacheConstants.LOGIN_APP_TOKEN_KEY + invitationCode) ? AjaxResult.success() : AjaxResult.error();
    }


    /*
     * @==============History===============<br/>
     * @Description //   小程序获取订单列表
     * @Date 2025-3-18 21:27
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.page.TableDataInfo
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "获取订单列表", sourceType = "controller")
    @GetMapping(value = "/getOrderList",produces = "application/json;charset=utf-8")
    public TableDataInfo getOrderList(@RequestParam Map<String, Object> params)
    {
        startPage();
        List<OrderInfo> list = orderService.getOrderList(params);
        return getDataTable(list);
    }


    /*
     * @==============History===============<br/>
     * @Description //   删除订单(取消订单)
     * @Date 2025-3-18 21:42
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "删除订单(取消订单)", sourceType = "controller")
    @DeleteMapping(value = "/deleteOrderInfo",produces = "application/json;charset=utf-8")
    public AjaxResult deleteOrderInfo(@RequestParam("id") Long id)
    {
        return orderService.deleteOrderInfo(id);
    }


    /*
     * @==============History===============<br/>
     * @Description //   获取用户推广记录
     * @Date 2025-3-18 22:21
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.page.TableDataInfo
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "获取订单列表", sourceType = "controller")
    @GetMapping(value = "/getPromotionList",produces = "application/json;charset=utf-8")
    public TableDataInfo getPromotionList(@RequestParam Map<String, Object> params)
    {
        startPage();
        List<PromotionInfo> list = userService.getPromotionList(params);
        return getDataTable(list);
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户文章点赞
     * @Date 2025-3-23 9:20
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户点赞文章", sourceType = "controller")
    @GetMapping(value = "/upArticleInfoThumbs",produces = "application/json;charset=utf-8")
    public AjaxResult upArticleInfoThumbs(@RequestParam("id") Long id)
    {
        return userService.upArticleInfoThumbs(id);
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户取消点赞
     * @Date 2025-3-23 9:31
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户取消点赞文章", sourceType = "controller")
    @GetMapping(value = "/downArticleInfoThumbs",produces = "application/json;charset=utf-8")
    public AjaxResult downArticleInfoThumbs(@RequestParam("id") Long id)
    {
        return userService.downArticleInfoThumbs(id);
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户评论文章
     * @Date 2025-3-24 19:58
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户评论文章", sourceType = "controller")
    @PostMapping(value = "/insertArticleCommentInfo",produces = "application/json;charset=utf-8")
    public AjaxResult insertArticleCommentInfo(@RequestBody ArticleComment articleComment)
    {
        return userService.insertArticleCommentInfo(articleComment);
    }


    /*
     * @==============History===============<br/>
     * @Description //   删除用户评论文章
     * @Date 2025-3-24 20:05
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户删除评论文章", sourceType = "controller")
    @DeleteMapping(value = "/deleteArticleCommentInfo",produces = "application/json;charset=utf-8")
    public AjaxResult deleteArticleCommentInfo(@RequestParam("id") Long id)
    {
        return userService.deleteArticleCommentInfo(id);
    }




    /*
     * @==============History===============<br/>
     * @Description //   用户行程点赞
     * @Date 2025-3-23 9:20
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户行程点赞", sourceType = "controller")
    @GetMapping(value = "/upTripInfoThumbs",produces = "application/json;charset=utf-8")
    public AjaxResult upTripInfoThumbs(@RequestParam("id") Long id)
    {
        return userService.upTripInfoThumbs(id);
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户取消行程点赞
     * @Date 2025-3-23 9:31
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户取消行程点赞", sourceType = "controller")
    @GetMapping(value = "/downTripInfoInfoThumbs",produces = "application/json;charset=utf-8")
    public AjaxResult downTripInfoInfoThumbs(@RequestParam("id") Long id)
    {
        return userService.downTripInfoInfoThumbs(id);
    }



    /*
     * @==============History===============<br/>
     * @Description //   用户评论行程
     * @Date 2025-3-24 19:58
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户评论行程", sourceType = "controller")
    @PostMapping(value = "/insertTripInfoCommentInfo",produces = "application/json;charset=utf-8")
    public AjaxResult insertTripInfoCommentInfo(@RequestBody TripComment tripComment)
    {
        return userService.insertTripInfoCommentInfo(tripComment);
    }


    /*
     * @==============History===============<br/>
     * @Description //   删除用户评论文章
     * @Date 2025-3-24 20:05
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户删除评论行程", sourceType = "controller")
    @DeleteMapping(value = "/deleteTripInfoCommentInfo",produces = "application/json;charset=utf-8")
    public AjaxResult deleteTripInfoCommentInfo(@RequestParam("id") Long id)
    {
        return userService.deleteTripInfoCommentInfo(id);
    }



    /*
     * @==============History===============<br/>
     * @Description //   获取省市区
     * @Date 2025-4-6 1:40
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.util.List<com.ruoyi.app.dto.Area>
     */
    @GetMapping(value = "/getAreaInfo",produces = "application/json;charset=utf-8")
    public List<Area> getAreaInfo() throws IOException
    {

        // 1. 通过类路径获取资源文件
        ClassPathResource resource = new ClassPathResource("templates/regions.json");

        // 2. 创建ObjectMapper实例
        ObjectMapper mapper = new ObjectMapper();

        // 3. 解析JSON到Java对象
        return  mapper.readValue(
                resource.getInputStream(),
                mapper.getTypeFactory().constructCollectionType(List.class, Area.class)
        );
    }



    /*
     * @==============History===============<br/>
     * @Description //   小程序访问
     * @Date 2025-4-7 23:03
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @GetMapping(value = "/getVisit",produces = "application/json;charset=utf-8")
    public AjaxResult getVisit()
    {
        return userService.getVisit();
    }



    /*
     * @==============History===============<br/>
     * @Description //   用户访问
     * @Date 2025-4-7 23:03
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户访问小程序", sourceType = "controller")
    @GetMapping(value = "/getUserVisit",produces = "application/json;charset=utf-8")
    public AjaxResult getUserVisit()
    {
        return userService.getUserVisit();
    }




    /*
     * @==============History===============<br/>
     * @Description //   用户绑定邀请码
     * @Date 2025-4-14 23:41
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户绑定邀请码", sourceType = "controller")
    @GetMapping(value = "/band",produces = "application/json;charset=utf-8")
    public AjaxResult band(@RequestParam("code") String code)
    {
        return userService.band(code);
    }



    /*
     * @==============History===============<br/>
     * @Description //   用户获取行程消息列表
     * @Date 2025-4-15 0:00
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户获取行程消息列表", sourceType = "controller")
    @GetMapping(value = "/messageList",produces = "application/json;charset=utf-8")
    public AjaxResult messageList()
    {
        return userService.messageList();
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户更新行程消息列表
     * @Date 2025-4-15 0:32
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户更新行程消息列表", sourceType = "controller")
    @PostMapping(value = "/delMessageList",produces = "application/json;charset=utf-8")
    public AjaxResult delMessageList(@RequestBody DelMessageDto delMessageDto)
    {
        return userService.delMessageDto(delMessageDto);
    }



    /*
     * @==============History===============<br/>
     * @Description //   用户收藏文章,行程
     * @Date 2025-4-17 18:41
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户收藏", sourceType = "controller")
    @PostMapping(value = "/collect",produces = "application/json;charset=utf-8")
    public AjaxResult collect(@RequestBody CollectDto collectDto)
    {
        return userService.collect(collectDto);
    }


    /*
     * @==============History===============<br/>
     * @Description //   获取用户收藏列表
     * @Date 2025-4-17 19:00
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "获取用户收藏列表", sourceType = "controller")
    @GetMapping(value = "/collectList",produces = "application/json;charset=utf-8")
    public AjaxResult collectList()
    {
        return userService.collectList();
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户取消收藏
     * @Date 2025-4-17 19:31
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PermissionAnnotate(operateType = "用户模块", operateExplain = "用户取消收藏", sourceType = "controller")
    @DeleteMapping(value = "/cancelCollect",produces = "application/json;charset=utf-8")
    public AjaxResult cancelCollect(@RequestParam("collectId") Long collectId)
    {
        return userService.cancelCollect(collectId);
    }

}
